var defaultPage = Public.getDefaultPage();
var config = defaultPage.CONFIG;
var system = defaultPage.SYSTEM;
var isRefresh = false;
var queryurl = config.BASEPATH+'sell/allocate/page';
var _height = $(parent).height()-222,_width = $(parent).width()-34;

var Utils = {
	doQueryShop : function(type,code,name){
		var title = '';
		if(type == 'out'){
			title = '选择调出店铺';
		}else if(type == 'in'){
			title = '选择调入店铺';
		}
		commonDia = $.dialog({
			title : title,
			content : 'url:'+config.BASEPATH+'base/shop/to_list_dialog_bymodule',
			data : {multiselect:false,module:'sell_allocate'},
			width : 450,
			height : 320,
			max : false,
			min : false,
			cache : false,
			lock: true,
			ok:function(){
				var selected = commonDia.content.doSelect();
				if (!selected) {
					return false;
				}
				$("#"+code).val(selected.sp_code);
				$("#"+name).val(selected.sp_name);
			},
			close:function(){
				if(commonDia.content.dblClick){//双击关闭
					var selected = commonDia.content.doSelect();
					$("#"+code).val(selected.sp_code);
					$("#"+name).val(selected.sp_name);
				}
			},
			cancel:true
		});
	},
	doQueryEmp : function(){
		commonDia = $.dialog({
			title : '选择经办人',
			content : 'url:'+config.BASEPATH+'cash/emp/to_emp_dialog',
			data : {multiselect:false},
			width : 450,
			height : 320,
			max : false,
			min : false,
			cache : false,
			lock: true,
			ok:function(){
				var selected = commonDia.content.doSelect();
				if (!selected) {
					return false;
				}
				$("#ac_man").val(selected.em_name);
			},
			close:function(){
				if(commonDia.content.dblClick){//双击关闭
					var selected = commonDia.content.doSelect();
					$("#ac_man").val(selected.em_name);
				}
			},
			cancel:true
		});
	}
};

var handle = {
	operate: function(oper, id){//修改、新增
		if(oper == 'add'){
			url = config.BASEPATH+"sell/allocate/to_add";
			data = {oper: oper, callback:this.callback};
		}else if(oper == 'edit'){
			url = config.BASEPATH+"sell/allocate/to_update?ac_id="+id;
			data = {oper: oper,callback: this.callback};
		}else if(oper == 'view'){
			url = config.BASEPATH+"sell/allocate/to_view?ac_id="+id;
			data = {oper: oper,callback: this.callback};
		}else if(oper == 'out' || oper == "in" || oper == "confirm"){
			url = config.BASEPATH+"sell/allocate/to_view?ac_id="+id;
			data = {oper: oper,callback: this.callback};
		}else if(oper == 'report'){
			url = config.BASEPATH+"sell/allocate/to_report";
			data = {oper: oper,callback: this.callback};
		}else{
			return false;
		}
		$.dialog({
			title : false,
			content : 'url:'+url,
			data: data,
			max : false,
			min : false,
			cache : false,
			lock: false
		}).max();
	},
	del: function(rowId){//删除
		var rowData = $("#grid").jqGrid("getRowData", rowId);
	 	$.dialog.confirm('数据删除后无法恢复，确定要清除吗？', function(){
			$.ajax({
				type:"POST",
				url:config.BASEPATH+'sell/allocate/del',
				data:{"number":rowData.ac_number},
				cache:false,
				dataType:"json",
				success:function(data){
					if(undefined != data && data.stat == 200){
						Public.tips({type: 3, content : '删除成功!'});
						$('#grid').jqGrid('delRowData', rowId);
					}else{
						Public.tips({type: 1, content : data.message});
					}
				}
			});
		});	
    },
    callback: function(data, oper, dialogWin){
		if(oper == "edit") {
			THISPAGE.reloadData();
			dialogWin && dialogWin.api.close();
		}
		if(oper == "out" || oper == "in" || oper == "confirm") {
			$("#grid").jqGrid('setRowData', data.id, data);
			dialogWin && dialogWin.api.close();
		}
		if(oper == "add"){
			THISPAGE.reloadData();
			dialogWin && dialogWin.api.close();
		}
		if(oper == 'view'){
			THISPAGE.reloadData();
			dialogWin && dialogWin.api.close();
		}
	},
	operFmatter : function(val, opt, row){
		var btnHtml = '';
		if (row.ac_state == '0') {//暂存
			if(row.ac_out_shop == system.SHOP_CODE){//调出店铺为本店铺,则有调出操作
				btnHtml += '<input type="button" value="调出" class="btn_sp" onclick="javascript:handle.operate(\'out\',' + opt.rowId + ');" />';
				btnHtml += '<input type="button" value="修改" class="btn_xg" onclick="javascript:handle.operate(\'edit\',' + opt.rowId + ');" />';
			}else{//查看
				btnHtml += '<input type="button" value="查看" class="btn_ck" onclick="javascript:handle.operate(\'view\',' + opt.rowId + ');" />';
			}
        }else if (row.ac_state == '1') {//已调出
        	if(row.ac_in_shop == system.SHOP_CODE){//调入店铺为本店铺,则有调入操作
        		btnHtml += '<input type="button" value="调入" class="btn_qh" onclick="javascript:handle.operate(\'in\',' + opt.rowId + ');" />';
        	}else{
        		btnHtml += '<input type="button" value="查看" class="btn_ck" onclick="javascript:handle.operate(\'view\',' + opt.rowId + ');" />';
        	}
        }else if (row.ac_state == '3') {//已拒收
        	if(row.ac_out_shop == system.SHOP_CODE){//调出店铺为本店铺,则有确认操作
        		btnHtml += '<input type="button" value="确认" class="btn_xg" onclick="javascript:handle.operate(\'confirm\',' + opt.rowId + ');" />';
        	}else{
        		btnHtml += '<input type="button" value="查看" class="btn_ck" onclick="javascript:handle.operate(\'view\',' + opt.rowId + ');" />';
        	}
        }else if (row.ac_state == '2') {//完成
        	btnHtml += '<input type="button" value="查看" class="btn_ck" onclick="javascript:handle.operate(\'view\',' + opt.rowId + ');" />';
        } 
		return btnHtml;
	},
	formatArState:function(val, opt, row){
		if(val == 0){
			return '暂存';
		}else if(val == 1){
			return '已调出';
		}else if(val == 2){
			return '完成';
		}else if(val == 3){
			return '已拒收';
		}
		return val;
	}
};
var THISPAGE = {
	init:function (){
		this.initDom();
		this.initGrid();
		this.initEvent();	
	},
	initDom:function(){
		this.$_date = $("#date").cssRadio({ callback: function($_obj){
			dateRedioClick($_obj.find("input").attr("id"));
		}});
		this.$_ar_state = $("#ar_state").cssRadio({ callback: function($_obj){
			$("#ac_state").val($_obj.find("input").val());
		}});
		this.$_out_in = $("#td_out_in").cssRadio({ callback: function($_obj){
			var type = $_obj.find("input").val();
			if(type == "out"){//调出
				$("#outshop_name").val(system.SHOP_NAME);
				$("#ac_out_shop").val(system.SHOP_CODE);
				$("#inshop_name").val("");
				$("#ac_in_shop").val("");
				$("#btn_out_shop").attr("disabled",true);
				$("#outshop_name").attr("disabled",true);
				$("#btn_in_shop").attr("disabled",false);
				$("#inshop_name").attr("disabled",false);
				
			}else if(type == "in"){//调入
				$("#inshop_name").val(system.SHOP_NAME);
				$("#ac_in_shop").val(system.SHOP_CODE);
				$("#outshop_name").val("");
				$("#ac_out_shop").val("");
				$("#btn_in_shop").attr("disabled",true);
				$("#inshop_name").attr("disabled",true);
				$("#btn_out_shop").attr("disabled",false);
				$("#outshop_name").attr("disabled",false);
			}else{
				$("#outshop_name").val("");
				$("#ac_out_shop").val("");
				$("#inshop_name").val("");
				$("#ac_in_shop").val("");
				$("#btn_in_shop").attr("disabled",false);
				$("#outshop_name").attr("disabled",false);
				$("#btn_out_shop").attr("disabled",false);
				$("#inshop_name").attr("disabled",false);
			}
		}});
	},
	gridTotal:function(){
    	var grid=$('#grid');
		var ac_amount=grid.getCol('ac_amount',false,'sum');
    	var ac_sell_money=grid.getCol('ac_sell_money',false,'sum');
    	grid.footerData('set',{ac_number:'合计：',ac_amount:ac_amount,ac_sell_money:ac_sell_money});
    },
	initGrid:function(){
		var colModel = [
	    	{label:'操作',name: 'operate',width: 100, formatter: handle.operFmatter,align:'center', sortable:false},
	    	{label:'',name: 'ac_out_shop',index: 'ac_out_shop',width:100,hidden:true},
	    	{label:'',name: 'ac_in_shop',index: 'ac_in_shop',width:100,hidden:true},
	    	{label:'单据编号',name: 'ac_number',index: 'ac_number',width:150},
	    	{label:'调拨日期',name: 'ac_date',index: 'ac_date',width:80},
	    	{label:'经办人',name: 'ac_man',index: 'ac_man',width:100},
	    	{label:'调出店铺',name: 'outshop_name',index: 'ac_out_shop',width:120},
	    	{label:'调入店铺',name: 'inshop_name',index: 'ac_in_shop',width:120},
	    	{label:'总计数量',name: 'ac_amount',index: 'ac_amount',width:80,align:'right'},
	    	{label:'零售金额',name: 'ac_sell_money',index: 'ac_sell_money',width:80,align:'right',formatter: Public.formatMoney},
	    	{label:'状态',name: 'ac_state',index: 'ac_state',width:80,formatter: handle.formatArState,align:'center'},
	    	{label:'接收人',name: 'ac_receiver',index: 'ac_receiver',width:100},
	    	{label:'接收日期',name: 'ac_recedate',index: 'ac_recedate',width:100}
	    ];
		$('#grid').jqGrid({
            url:queryurl+'?'+THISPAGE.buildParams(),			
			datatype: 'json',
			width: _width,
			height: _height,
			gridview: true,
			colModel: colModel,
			rownumbers: true,//行号
			pager: '#page',//分页
			pgbuttons:true,
			recordtext:'{0} - {1} 共 {2} 条',  
			multiselect:false,//多选
			viewrecords: true,
			rowNum:config.BASEROWNUM,//每页条数
			rowList:config.BASEROWLIST,//分页条数
			cmTemplate: {sortable:true,title:false},
			shrinkToFit:false,//表格是否自动填充
			footerrow: true,
			jsonReader: {
				root: 'data.list',
				total: 'data.pageInfo.totalPage',
                page: 'data.pageInfo.currentPage',
                records: 'data.pageInfo.totalRow',
				repeatitems : false,
				id: 'ac_id'  //图标ID
			},
			loadComplete: function(data){
				THISPAGE.gridTotal();
			},
			loadError: function(xhr, status, error){		
			}
	    });
	},
	buildParams : function(){
		var params = '';
		params += 'ac_state='+$("#ac_state").val();
		params += '&begindate='+$("#begindate").val();
		params += '&enddate='+$("#enddate").val();
		params += '&ac_out_shop='+$("#ac_out_shop").val();
		params += '&ac_in_shop='+$("#ac_in_shop").val();
		params += '&ac_man='+Public.encodeURI($("#ac_man").val());
		params += '&ac_number='+Public.encodeURI($("#ac_number").val());
		return params;
	},
	reset:function(){
		$("#outshop_name").val("");
		$("#inshop_name").val("");
		$("#ac_out_shop").val("");
		$("#ac_in_shop").val("");
		$("#ac_number").val("");
		$("#ac_man").val("");
		THISPAGE.$_date.setValue(0);
		THISPAGE.$_ar_state.setValue(0);
		$("#begindate").val("");
		$("#enddate").val("");
	},
	reloadData:function(){
		var param=THISPAGE.buildParams();
		$("#grid").jqGrid('setGridParam',{datatype:"json",page:1,url:queryurl+'?'+param}).trigger("reloadGrid");
	},
	initEvent:function(){
		//查询
		$('#btn-search').on('click', function(e){
			e.preventDefault();
			THISPAGE.reloadData();
		});
		$('#btn_ok').on('click', function(e){
			e.preventDefault();
			Public.hideConstion();
			THISPAGE.reloadData();
		});
		//新增
		$('#btn-add').on('click', function(e){
			e.preventDefault();
			handle.operate('add');
		});
		$('#btn-report').on('click', function(e){
			e.preventDefault();
			handle.operate('report');
		});
		$('#btn-del').on('click', function(e){
			e.preventDefault();
			var rowId = $('#grid').jqGrid('getGridParam','selrow');
			if(rowId == null || rowId == ''){
				Public.tips({type: 1, content : "您未选择任何数据"});
				return;
			}
			var rowData =$("#grid").jqGrid("getRowData", rowId);
			if(rowData.ac_state != "暂存"){
				Public.tips({type: 1, content : "单据已发货，不能删除"});
				return;
			}
			if(rowData.ac_out_shop != system.SHOP_CODE){
				Public.tips({type: 1, content : "该单据不是本店铺新增，不能删除"});
				return;
			}
			handle.del(rowId)
		});
		$('#btn_reset').on('click', function(e){
			e.preventDefault();
			THISPAGE.reset();
		});
	}
}
THISPAGE.init();